package bmptest.bmp.controller.SysUser;

import bmptest.bmp.entity.SysUser;
import bmptest.bmp.service.SysUser.SysUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lyq
 * @date 2021/9/6 9:50
 * 用户管理模块
 */

@RestController
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    //新增
    @CrossOrigin
    @RequestMapping(value = "/api/addUser", method = RequestMethod.POST)
    public String  addUser(@RequestBody SysUser sUser) throws Exception{
        String isture = sysUserService.addAll(sUser);
        return isture;
    }

    //删除
    @CrossOrigin
    @RequestMapping(value = "/api/deleteUser",method = RequestMethod.POST)
    public void deleteUser(@RequestBody SysUser sysUser){
        sysUserService.deleteUser(sysUser.getUser_id());
    }

    //更新
    @CrossOrigin
    @RequestMapping(value = "/api/updateUser",method = RequestMethod.POST)
    public Map<String, Object> updateUser(@RequestBody SysUser sysUser){
        String isvalidate = sysUserService.updateUser(sysUser);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sysUser",sysUser);
        map.put("isvalidate",isvalidate);
        return map;
    }

    //更新状态
    @CrossOrigin
    @RequestMapping(value = "/api/updateStatus",method = RequestMethod.POST)
    public SysUser updateStatus(@RequestBody SysUser sysUser){
        sysUserService.updateStatus(sysUser);
        return sysUser;
    }

    //多条件搜索
    @CrossOrigin
    @RequestMapping(value = "/api/searchUser",method = RequestMethod.GET)
    public Map<String, Object> searchUser(@RequestParam("user_name") String username,@RequestParam("status") Integer status,
                                    @RequestParam("begindate") String begindate,@RequestParam("enddate") String enddate,
                                    @RequestParam("pageNum") String pageNum, @RequestParam("pageSize") String pageSize){
        List<SysUser> pagelist = sysUserService.searchUser(username,status,begindate,enddate);
        Integer pn = Integer.parseInt(pageNum);
        Integer ps = Integer.parseInt(pageSize);
        if(!"null".equals(String.valueOf(pn))&&!"null".equals(String.valueOf(ps))){
            PageHelper.startPage(pn, ps);
        }
        List<SysUser> page = sysUserService.searchUser(username,status,begindate,enddate);
        Integer size = pagelist.size();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pagelist",page);
        map.put("totalsize",size);
        return map;
    }
}
